/*!\file  task.c  The tasks implementation.
 *
 *      This file is part of the micro-rtos package.
 *
 *  Copyright (c) 2008-2009 by 
		Gabriel Zabusek <gabriel.zabusek@gmail.com>
 *
 *      This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License
 *      as published by the Free Software Foundation; either version
 *      2 of the License, or (at your option) any later version.
 */

#ifndef __TASK_H
#define __TASK_H

#define TASK_STACK_SIZE 128


#include <include/types.h>
#include <kernel/list.h>

typedef enum TASK_STATE {
	UNDEFINED = 0x00,	//this may be used to see if we need to restart the task

	RUNNING   = 0x01,
	STOPPED   = 0x02,
	READY	  = 0x04

} task_state_t;

typedef enum TASK_PRIORITY {

	PRIORITY_IDLE    = 0x00,
	PRIORITY_LOWEST  = 0x01,
	// ...some other priorities
	PRIORITY_LOW	 = 0x3F, // = 0x7F / 0x02
	// ...some other priorities
	PRIORITY_NORMAL  = 0x7F, // = 0xFF / 0x02 
	// ...
	PRIORITY_HIGH	 = 0xBE, // = 0x7F + 0x3F
	// ...
	PRIORITY_HIGHEST = 0xFF

} task_priority_t;

struct TASK;

typedef struct TASK_LIST{
	struct TASK * p_task;
	void *(*p_proc)(void *);
	struct list_head list;
} task_list_t;

typedef volatile struct TASK {
	volatile unative_t 	*pStackTop;
	volatile unative_t	StackBlock[TASK_STACK_SIZE];

	unsigned char 		nTaskId;
	task_state_t  		TaskState;
	unsigned int  		nRunTimeCurrent;
	//unsigned int  	nRunTimeTotal; // this is probably not neccesary, we are saving space here - not doin stats
	task_priority_t 	TaskPriority;
	bool_t	      		bTaskInCriticalSection;	

	//we dont have malloc so have to do it this ugly way
	task_list_t		list_item;

} task_t;

volatile task_list_t ready_list;

//global vars
volatile unative_t * pCurTaskStackTop;
volatile task_t    * pCurRunningTask;

//global procs
void init_task(void);
int task_create( task_t * pTask, void *(*task_proc)(void *) );
extern void * def_link(void *d);

#endif
